package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    TextView textView;
    EditText editText1,editText2,editText,editText3;
    Button button,button1;
    String user_name,responseData;
    String user_password,user_repassword;
    List<Integer> registerlist;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    for(Integer result:registerlist){
                        if(result==1){
                            alterDialog();
                        }else {
                            Toast.makeText(getApplicationContext(),"用户名已存在",Toast.LENGTH_SHORT).show();
                        }
                    }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvent();
    }

    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user_name=editText.getText().toString();
                user_password=editText1.getText().toString();
                user_repassword=editText2.getText().toString();
                if(user_password.equals(user_repassword)) {
                    HttpUtil.sendOkHttpRequest("http://47.101.175.33:8080/AndroidDataFinalTest/user_findName.action?user_name=" + user_name + "&&user_password=" + user_password, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            responseData = response.body().string();
                            parseJsonWithGsonObject(responseData);
                        }
                    });
                }else{
                    Toast.makeText(getApplicationContext(),"两次密码输入不同",Toast.LENGTH_SHORT).show();
                }
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void parseJsonWithGsonObject(String responseData) {
        Gson gson = new Gson();
        registerlist = gson.fromJson(responseData, new TypeToken<List<Integer>>() {
        }.getType());
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);
    }

    private void initView() {
        editText=findViewById(R.id.register_username);
        editText1=findViewById(R.id.register_password);
        editText2=findViewById(R.id.register_repassword);
        button=findViewById(R.id.register);
        textView=findViewById(R.id.register_to_login);
    }
    private void alterDialog(){
        AlertDialog.Builder dialog=new AlertDialog.Builder(RegisterActivity.this);
        dialog.setTitle("注册成功");
        dialog.setMessage("点击确定返回登录页面");
        dialog.setCancelable(false);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
